package com.yx.cloud.activity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.core.content.ContextCompat;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.product.sdk.tmall.AliGenieSdk;
import com.yx.cloud.product.sdk.tmall.DeviceStatus;
import com.yx.cloud.product.sdk.tmall.MusicInfo;
import com.yx.cloud.utils.DateUtils;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.StatusBarColorUtils;
import com.yx.cloud.utils.ToastUtil;
import com.yx.cloud.view.MusicBottomSheetDialog;
import com.iot.common.utils.Event;
import java.lang.ref.SoftReference;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import de.hdodenhof.circleimageview.CircleImageView;

public class MusicPlayingActivity extends BaseActivity implements View.OnClickListener{

    public static volatile boolean requestPlayingItem = false;
    public static volatile boolean refreshDeviceStatus = false;
    private ImageView ivReturn;
    private ImageView ivRightImage;
    private TextView tvMusicTitle;
    private CircleImageView ciMusicCover;
    private ImageView ivRecordArm;
    private TextView tvMusicTitleBlow;
    private TextView tvMusicSinger;
    private SeekBar sbProgress;
    private TextView tvPlayingProgress;
    private TextView tvTotalProgress;
    private ImageView ivPlayingMode;
    private ImageView ivPreMusic;
    private ImageView ivNextMusic;
    private ImageView ivPlayMusic;
    private ImageView ivPlayingList;
    private ImageView ivMuted;
    private SeekBar sbMusicVolumeValue;
    private LinearLayout llMusicVolumeIcon;
    private MusicInfo musicInfo;
    private DeviceStatus deviceStatus;
    private ObjectAnimator discObjectAnimator;
    private ObjectAnimator armObjectAnimator;
    private boolean playStatusChange = false;
    private String CurrentPlayStatus = "finish";
    private Handler handler;
    private int lastProgress = 0;
    private int currentProgress = 0;
    int playMode = 3;
    boolean muted = false;
    boolean changeVolume = true;
    boolean progressControl = true;
    int volumeCount = 0;
    int progressCount = 0;
    MusicBottomSheetDialog musicBottomSheetDialog;
    private RequestOptions options;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.musicplaying_activity);
        StatusBarColorUtils.setStatusBarColor(this, R.color.status_bar_color, "1");
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        deviceStatus = AliGenieSdk.get().getCurrentMusic();
        Intent intent = getIntent();
        String musicInfoStr = intent.getStringExtra("musicInfo");
        musicInfo = new Gson().fromJson(musicInfoStr, MusicInfo.class);
        if(musicInfo != null){
            musicInfo.setMusicPlayStatus("pause");
            deviceStatus.getMusicInfo().setMusicPlayStatus("pause");
        }
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(musicBottomSheetDialog != null){
            musicBottomSheetDialog.dismiss();
        }
    }

    /**
     * 状态栏
     *themeBg：颜色值（在color设置直接调用即可）
     */
    @SuppressLint("InlinedApi")
    public void initSystemBar(Context mContext, int themeBg) {
        if (Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            window.setStatusBarColor(ContextCompat.getColor(mContext, themeBg));

            //状态栏颜色接近于白色，文字图标变成黑色
            View decor = window.getDecorView();
            int ui = decor.getSystemUiVisibility();
            //light --> a|=b的意思就是把a和b按位或然后赋值给a,   按位或的意思就是先把a和b都换成2进制，然后用或操作，相当于a=a|b
            ui = ui | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            decor.setSystemUiVisibility(ui);
        }
    }

    public static class MainHandler extends Handler {
        SoftReference<MusicPlayingActivity> musicPlayingActivitySoftReference;

        public MainHandler(MusicPlayingActivity activity) {
            musicPlayingActivitySoftReference = new SoftReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == Event.MESSAGE_TYPE_GET_CURRENT_PLAYING_ITEM_REPEAT) {
                if (musicPlayingActivitySoftReference.get() != null) {
                    musicPlayingActivitySoftReference.get().getPlayItem();
                }
            }
            if(msg.what == Event.MESSAGE_TYPE_GET_REFRESH_PROGRESS){
                if (musicPlayingActivitySoftReference.get() != null) {
                    musicPlayingActivitySoftReference.get().updateStatus();
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        requestPlayingItem = true;
        refreshDeviceStatus = false;
        if(handler != null){
            handler.removeMessages(Event.MESSAGE_TYPE_GET_CURRENT_PLAYING_ITEM_REPEAT);
            handler.removeMessages(Event.MESSAGE_TYPE_GET_REFRESH_PROGRESS);
        }
    }

    private void initView(){
        ivReturn = findViewById(R.id.iv_music_playing_return);
        tvMusicTitle = findViewById(R.id.tv_music_playing_title);
        ciMusicCover = findViewById(R.id.iv_music_playing_icon);
        tvMusicTitleBlow = findViewById(R.id.tv_music_playing_name);
        tvMusicSinger = findViewById(R.id.tv_music_playing_singer);
        sbProgress = findViewById(R.id.volumeSetting_seekBar);
        tvPlayingProgress = findViewById(R.id.tv_playing_progress);
        tvTotalProgress = findViewById(R.id.tv_total_progress);
        ivPlayingMode = findViewById(R.id.iv_music_playing_mode);
        ivPreMusic = findViewById(R.id.iv_previous_song);
        ivNextMusic = findViewById(R.id.iv_next_song);
        ivPlayMusic = findViewById(R.id.iv_play_music);
        ivPlayingList = findViewById(R.id.iv_music_playing_list);
        llMusicVolumeIcon = findViewById(R.id.ll_music_volume_icon);
        sbMusicVolumeValue = findViewById(R.id.sb_music_volume_value);
        ivRecordArm = findViewById(R.id.iv_record_arm);
        ivMuted = findViewById(R.id.iv_volume_muted);
        ivRightImage = findViewById(R.id.iv_music_playing_right);
        sbProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        progressControl = false;
                        progressCount = 0;
                        currentProgress = seekBar.getProgress();
                        setMusicProgress(seekBar.getProgress());
                    }
                }).start();
            }
        });
        sbMusicVolumeValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        changeVolume = false;
                        volumeCount = 0;
                        setMusicVolumeValue(seekBar.getProgress(),muted);
                    }
                }).start();
            }
        });
        ivReturn.setOnClickListener(this);
        ivPlayingMode.setOnClickListener(this);
        ivPreMusic.setOnClickListener(this);
        ivNextMusic.setOnClickListener(this);
        ivPlayingList.setOnClickListener(this);
        llMusicVolumeIcon.setOnClickListener(this);
        ivPlayMusic.setOnClickListener(this);
        ivMuted.setOnClickListener(this);
        ivRightImage.setOnClickListener(this);

        if(deviceStatus != null){
            sbProgress.setMax(deviceStatus.getMusicInfo().getProgress());
            sbProgress.setProgress(deviceStatus.getMusicInfo().getProgress());
            tvPlayingProgress.setText(DateUtils.timeConversion(deviceStatus.getMusicInfo().getProgress()));
            tvTotalProgress.setText(DateUtils.timeConversion(deviceStatus.getMusicInfo().getProgress()));
            if(deviceStatus.getMusicInfo().getMusicPlayStatus().equals("play")){
                ivPlayMusic.setImageResource(R.mipmap.pause_music_icon);
            }else {
                ivPlayMusic.setImageResource(R.mipmap.play_music_icon);
            }
            sbMusicVolumeValue.setProgress(deviceStatus.getVolume());
            if(deviceStatus.isMuted()){
                muted = true;
                ivMuted.setImageLevel(2);
            }else {
                muted = false;
                ivMuted.setImageLevel(1);
            }
        }
    }

    private void initData(){
        discObjectAnimator = ObjectAnimator.ofFloat(ciMusicCover, "rotation", 0, 360);
        discObjectAnimator.setDuration(12000);
        //使ObjectAnimator动画匀速平滑旋转
        discObjectAnimator.setInterpolator(new LinearInterpolator());
        //无限循环旋转
        discObjectAnimator.setRepeatCount(-1);
        discObjectAnimator.setRepeatMode(ValueAnimator.RESTART);

        armObjectAnimator = ObjectAnimator.ofFloat(ivRecordArm, "rotation", 0, 20);
        armObjectAnimator.setDuration(500);
        //使ObjectAnimator动画匀速平滑旋转
        armObjectAnimator.setInterpolator(new LinearInterpolator());
        armObjectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if(deviceStatus != null){
                    if(deviceStatus.getMusicInfo().getMusicPlayStatus().equals("play")){
                        if(!discObjectAnimator.isRunning()){
                            discObjectAnimator.start();
                        }else if(discObjectAnimator.isPaused()){
                            discObjectAnimator.resume();
                        }
                    }
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        ivRecordArm.setPivotX(30);
        ivRecordArm.setPivotY(60);

        requestPlayingItem = true;
        refreshDeviceStatus = true;

        options = new RequestOptions()
                .placeholder(R.mipmap.music_cover_default)
                .bitmapTransform(new RoundedCorners(20));

        handler = new MusicPlayingActivity.MainHandler(this);
        handler.sendEmptyMessage(Event.MESSAGE_TYPE_GET_CURRENT_PLAYING_ITEM_REPEAT);
        handler.sendEmptyMessage(Event.MESSAGE_TYPE_GET_REFRESH_PROGRESS);
    }

    private void getPlayItem(){
        if(requestPlayingItem){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Log.d("music","get Playing item");
                    AliGenieSdk.get().getPlayingItem();
                }
            }).start();
            handler.removeMessages(Event.MESSAGE_TYPE_GET_CURRENT_PLAYING_ITEM_REPEAT);
            handler.sendEmptyMessageDelayed(Event.MESSAGE_TYPE_GET_CURRENT_PLAYING_ITEM_REPEAT, 3000);
        }
    }
    private void indexControlPlay(int index){
        AliGenieSdk.get().indexControlPlay(index);
    }
    private void previousAndNextControl(String cmd){
        AliGenieSdk.get().previousAndNext(cmd);
    }

    private void setMusicVolumeValue(int value,Boolean isMuted){
        AliGenieSdk.get().volumeControl(value,isMuted);
    }

    private void setMusicProgress(int progress){
        AliGenieSdk.get().progressControl(progress);
    }

    private void playAndPause(String cmd){
        AliGenieSdk.get().playAndPause(cmd);
    }

    private void playModeControl(String mode){
        AliGenieSdk.get().playModeControl(mode);
    }
    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_music_playing_right:
            case R.id.iv_music_playing_return:
                Intent_utils.backIntentFromTop(this);
                break;
            case R.id.iv_music_playing_list:
                musicBottomSheetDialog = new MusicBottomSheetDialog();
                musicBottomSheetDialog.show(getSupportFragmentManager(),"tag");
                break;
            case R.id.iv_previous_song:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        previousAndNextControl("PREVIOUS");
                    }
                }).start();
                break;
            case R.id.iv_next_song:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        previousAndNextControl("NEXT");
                    }
                }).start();
                break;
            case R.id.ll_music_volume_icon:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        setMusicVolumeValue(0,true);
                    }
                }).start();
                break;
            case R.id.iv_play_music:
                if(deviceStatus != null){
                    if(deviceStatus.getMusicInfo().getMusicPlayStatus().equals("play")){
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                playAndPause("Pause");
                                Log.d("music","====================play");
                            }
                        }).start();
                    }else if(deviceStatus.getMusicInfo().getMusicPlayStatus().equals("pause")){
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                playAndPause("Play");
                                Log.d("music","====================play");
                            }
                        }).start();
                    }else if(deviceStatus.getMusicInfo().getMusicPlayStatus().equals("finish")){
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                indexControlPlay(deviceStatus.getMusicInfo().getPos());
                                Log.d("music","====================finish");
                            }
                        }).start();
                    }
                }
                break;
            case R.id.iv_music_playing_mode:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if(playMode == 3){
                            playModeControl(getMusicMode(0));
                            Log.d("music","====================play mode 0");
                        }else {
                            playModeControl(getMusicMode(playMode + 1));
                            Log.d("music","====================play mode " + playMode + 1);
                        }
                    }
                }).start();
                break;
            case R.id.iv_volume_muted:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if(deviceStatus != null){
                            if(deviceStatus.getVolume() > 5){
                                changeVolume = false;
                                setMusicVolumeValue(0,true);
                                Log.d("music","==================== set volume" + deviceStatus.getVolume() + "muted is " + !muted);
                            }
                        }
                    }
                }).start();
                break;
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_PREVIOUS_AND_NEXT_CONTROL:
                if (event.isResult()) {
                    String cmd = (String)event.getExtra();
                    MusicInfo musicInfo = (MusicInfo)event.getContent();
                    if(musicInfo == null){
                        if(cmd.equals("NEXT")){
                            ToastUtil.show(this, getString(R.string.M_T_Last), Toast.LENGTH_SHORT);
                        }else {
                            ToastUtil.show(this, getString(R.string.M_T_First), Toast.LENGTH_SHORT);
                        }
                    }else {
                        updateDeviceStatus(musicInfo);
                        updateView(musicInfo);
                    }
                }else {
                    String message = (String)event.getContent();
                    ToastUtil.show(this, message, Toast.LENGTH_SHORT);
                }
                break;
            case Event.MESSAGE_TYPE_GET_CURRENT_PLAYING_ITEM:
                if (event.isResult()) {
                    musicInfo = (MusicInfo) event.getContent();
                    updateDeviceStatus(musicInfo);
                    updateView(musicInfo);
                }else {
                    String message = (String)event.getContent();
                    ToastUtil.show(this, message, Toast.LENGTH_SHORT);
                }
                break;
            case Event.MESSAGE_TYPE_GET_DEVICE_STATUS_DETAIL:
                if (event.isResult()) {
                    DeviceStatus status = (DeviceStatus) event.getContent();
                    if(musicInfo.getRawId().equals(status.getMusicInfo().getRawId())){
                        deviceStatus.setMusicInfo(musicInfo);
                        deviceStatus.getMusicInfo().setMusicPlayStatus(status.getMusicInfo().getMusicPlayStatus());
                        deviceStatus.getMusicInfo().setProgress(status.getMusicInfo().getProgress());
                        deviceStatus.setMuted(status.isMuted());
                        deviceStatus.setVolume(status.getVolume());
                        AliGenieSdk.get().setCurrentMusic(deviceStatus);
                    }else {
                        deviceStatus.getMusicInfo().setMusicPlayStatus("pause");
                    }

                    if(changeVolume == false){
                        volumeCount++;
                    }
                    if(volumeCount == 2){
                        changeVolume = true;
                        volumeCount = 0;
                    }

                    if(progressControl == false){
                        progressCount++;
                    }
                    if(progressCount == 2){
                        progressControl = true;
                        progressCount = 0;
                    }
                }else {
                    String message = (String)event.getContent();
                    Log.i("music", "设备状态详情" + message);
                    //ToastUtil.show(this, message, Toast.LENGTH_SHORT);
                }
                break;
            case Event.MESSAGE_TYPE_PLAY_MODE_CONTROL:
                if (event.isResult()) {
                    playMode = getMusicMode((String)event.getContent());
                    ivPlayingMode.setImageLevel(playMode);
                }else {
                    String message = (String)event.getContent();
                    ToastUtil.show(this, message, Toast.LENGTH_SHORT);
                }
                break;
            case Event.MESSAGE_TYPE_DEVICE_VOLUME_CONTROL:
            case Event.MESSAGE_TYPE_PROGRESS_CONTROL:
            case Event.MESSAGE_TYPE_PLAY_AND_PAUSE:
                if(!event.isResult()){
                    String message = (String)event.getContent();
                    ToastUtil.show(this, message, Toast.LENGTH_SHORT);
                }
                break;
        }
    }

    private void updateView(MusicInfo musicInfo){
        tvMusicTitle.setText(musicInfo.getTitle());
        tvMusicTitleBlow.setText(musicInfo.getTitle());
        tvMusicSinger.setText(musicInfo.getSingers());
        tvTotalProgress.setText(DateUtils.timeConversion(musicInfo.getAudioLength()));
        sbProgress.setMax(musicInfo.getAudioLength());
        //设置播放模式
        playMode = getMusicMode(musicInfo.getPlayMode());
        ivPlayingMode.setImageLevel(playMode);
        String icon = musicInfo.getCoverUrl();
        if(icon == null || icon.equals("")){
            ciMusicCover.setImageResource(R.mipmap.record_music_cover_default);
        }else {
            Glide.with(this).load(icon).apply(options).into(ciMusicCover);
        }
    }

    private void updateDeviceStatus(MusicInfo musicInfo){
        if(deviceStatus.getMusicInfo().getRawId().equals(musicInfo.getRawId())){
            deviceStatus.getMusicInfo().setPos(musicInfo.getPos());
            AliGenieSdk.get().setCurrentMusic(deviceStatus);
            Log.d("updateDeviceStatus","updateDeviceStatus ===" + musicInfo.getPos());
        }else {
            deviceStatus.getMusicInfo().setMusicPlayStatus("pause");
            deviceStatus.getMusicInfo().setProgress(0);
            deviceStatus.getMusicInfo().setAudioLength(musicInfo.getAudioLength());
        }
    }

    private int getMusicMode(String mode){
        if(mode == null){
            return 3;
        }
        if(mode.equals("Repeat")){
            return 1;
        }
        if(mode.equals("Shuffle")){
            return 0;
        }

        if(mode.equals("RepeatOne")){
            return 2;
        }
        return 3;
    }

    private String getMusicMode(int level){
        String mode = "Normal";
        switch (level){
            case 0:
                mode = "Shuffle";
                break;
            case 1:
                mode = "Repeat";
                break;
            case 2:
                mode = "RepeatOne";
                break;
            case 3:
                mode = "Normal";
                break;
        }
        return mode;
    }

    private void updateStatus(){
        if(deviceStatus != null){
            Log.d("updateStatus","get progress==*" + deviceStatus.getMusicInfo().getProgress());
            if(progressControl){
                if(lastProgress != deviceStatus.getMusicInfo().getProgress()){
                    lastProgress = deviceStatus.getMusicInfo().getProgress();
                    currentProgress = deviceStatus.getMusicInfo().getProgress();
                    Log.d("updateStatus","set progress==*" + currentProgress);
                }
            }

            if(!deviceStatus.getMusicInfo().getMusicPlayStatus().equals(CurrentPlayStatus)){
                playStatusChange = true;
                CurrentPlayStatus = deviceStatus.getMusicInfo().getMusicPlayStatus();
            }else {
                playStatusChange = false;
            }
            if(deviceStatus.getMusicInfo().getMusicPlayStatus().equals("play")){
                if(playStatusChange){
                    armObjectAnimator.start();
                    Log.d("updateStatus","pause === armObjectAnimator start==*" + currentProgress);
                }
                ivPlayMusic.setImageResource(R.mipmap.pause_music_icon);
                currentProgress++;
                Log.d("updateStatus","playing === refresh progress==*" + currentProgress);
            }else if(deviceStatus.getMusicInfo().getMusicPlayStatus().equals("pause")){
                if(playStatusChange){
                    if(discObjectAnimator.isRunning()){
                        discObjectAnimator.pause();
                        armObjectAnimator.reverse();
                        Log.d("updateStatus","pause === armObjectAnimator reverse==*" + currentProgress);
                    }
                }
                ivPlayMusic.setImageResource(R.mipmap.play_music_icon);
                Log.d("updateStatus","pause ==== refresh progress==*" + currentProgress);
            }else if(deviceStatus.getMusicInfo().getMusicPlayStatus().equals("finish")){
                if(playStatusChange){
                    if(discObjectAnimator.isRunning()){
                        armObjectAnimator.reverse();
                        discObjectAnimator.end();
                        Log.d("updateStatus","finish === armObjectAnimator reverse==*" + currentProgress);
                    }
                    ivPlayMusic.setImageResource(R.mipmap.play_music_icon);
                }
                Log.d("updateStatus","finish ==== refresh progress==*" + currentProgress);
            }

            if(musicInfo != null){
                if(currentProgress >= musicInfo.getAudioLength()){
                    currentProgress = musicInfo.getAudioLength();
                    /*ivPlayMusic.setImageResource(R.mipmap.play_music_icon);
                    if(playStatusChange){
                        if(discObjectAnimator.isRunning() && !discObjectAnimator.isPaused()){
                            armObjectAnimator.reverse();
                            discObjectAnimator.end();
                        }
                    }*/
                }
            }

            tvPlayingProgress.setText(DateUtils.timeConversion(currentProgress));
            sbProgress.setProgress(currentProgress);
            /*if (progressControl){
                tvPlayingProgress.setText(DateUtils.timeConversion(currentProgress));
                sbProgress.setProgress(currentProgress);
            }*/

           /* if(deviceStatus.isMuted()){
                muted = true;
                ivMuted.setImageLevel(2);
            }else {
                muted = false;
                ivMuted.setImageLevel(1);
            }*/
            if(deviceStatus.getVolume() <= 5){
                ivMuted.setImageLevel(2);
            }else {
                ivMuted.setImageLevel(1);
            }

            if(changeVolume){
                sbMusicVolumeValue.setProgress(deviceStatus.getVolume());
            }

        }else if(musicInfo != null){
            tvPlayingProgress.setText(DateUtils.timeConversion(musicInfo.getProgress()));
            tvTotalProgress.setText(DateUtils.timeConversion(musicInfo.getAudioLength()));
            sbProgress.setMax(musicInfo.getAudioLength());
            sbProgress.setProgress(musicInfo.getProgress());
        }
        Log.d("music","refresh progress");
        handler.removeMessages(Event.MESSAGE_TYPE_GET_REFRESH_PROGRESS);
        handler.sendEmptyMessageDelayed(Event.MESSAGE_TYPE_GET_REFRESH_PROGRESS, 1000);
    }
}
